#include "urlrewrite.h"

int folder_redirect(char *s1, int * s1_len, const char *s)
{
	/* if the request folder is not tailed by '/'
 		send back 302 with new location of folder with '/' ending
		except that s="." (root)

		however, when s=".", this function will never be called
	*/
	if (s[0] !='.')
  (*s1_len) += sprintf (s1 + (*s1_len), "HTTP/1.1 301 Moved Permanently\r\nServer: clue\r\nContent-Type: text/html\r\nConnection: close\r\nLocation: /%s/\r\n\r\n", s);
	return 0;
}
int
listdir (char *s1, int *s1_len, const char *s)
{
  DIR *dp;
  struct dirent *ep[1000];
	struct dirent *tempep;
	int i, j, cnt;

//printf("s=%s\n", s);

	if (s[strlen(s)-1]!='/' && s[0] !='.')
	{
		
  (*s1_len) += sprintf (s1 + (*s1_len), "HTTP/1.1 301 Moved Permanently\r\nServer: clue\r\nContent-Type: text/html\r\nConnection: close\r\nLocation: /%s/\r\n\r\n", s);
	return 0;
	}

	/* otherwise, continue listing index of folder
 */
  
  dp = opendir (s);
  if (dp != NULL)
  {
	(*s1_len) +=
    sprintf (s1 + (*s1_len),
	     "HTTP/1.1 200 OK\r\nServer: clue\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n");
  if (*s == '.')
    (*s1_len) += sprintf (s1 + (*s1_len), "<h2>Index of / </h2>\n");
  else
    (*s1_len) += sprintf (s1 + (*s1_len), "<h2>Index of /%s </h2>\n", s);


  if (!(*s == '.'))
    (*s1_len) += sprintf (s1 + (*s1_len), "<a href=\"..\">..</a><br>\n");


		/* read only first 1000 items from dir */
		i=0;
    while (i<1000 && (ep[i] = readdir (dp))) 
		{
			/* directory starting with '.' will not be shown
  			 (except for "." and ".." )
			*/
      if ( (* ep[i]->d_name) != '.')
				i++;
		}
			cnt=i;

    (void) closedir (dp);

		/* sort them by ascending alphabetic order */

		for (i=0;i<cnt;i++)
			for (j=i+1;j<cnt;j++)
				if (strcmp(ep[i]->d_name, ep[j]->d_name)>0)
				{
					tempep=ep[i];
					ep[i]=ep[j];
					ep[j]=tempep;
				}

		for (i=0;i<cnt;i++)
    {
		/* since we guaranteed that s is always tailed with '/'
  		there's no need to put '/' between s and ep->d_name
			special case: s='.' (root dir)
		 */
  if (!(*s == '.'))
	  (*s1_len) += sprintf (s1 + (*s1_len), "<a href=\"/%s%s\">%s</a><br>\n", s, ep[i]->d_name, ep[i]->d_name);
	else
	  (*s1_len) += sprintf (s1 + (*s1_len), "<a href=\"/%s\">%s</a><br>\n", ep[i]->d_name, ep[i]->d_name);
    }
  }
  else
    exit (-1);
  return 0;
}

int
urlrewrite (const char *s, char *s2)
{

  DIR *dp;
  FILE *fp;
  if (!(fp = fopen (s, "r")))
    return URL_NOTFOUND;
  fclose (fp);

  dp = opendir (s);
  if (dp)
  {
		
		/* if s is not tailed with '/', then return URL_FOLDER_REDIRECT
			so redirectdir can redirect the request
			except that s="." (root dir)
		*/ 
		if (s[strlen(s)-1]!='/' && s[0]!='.')
			return URL_FOLDER_REDIRECT;

		/* since s already has trailing '/', no need to add '/'
 */
    sprintf (s2, "%sindex.html", s);
    if ((fp = fopen (s2, "r")))
      return URL_FILE;

    sprintf (s2, "%sindex.htm", s);
    if ((fp = fopen (s2, "r")))
      return URL_FILE;

    sprintf (s2, "%sindex.php", s);
    if ((fp = fopen (s2, "r")))
      return URL_FILE;

    return URL_FOLDER;
  }
  else
    sprintf (s2, "%s", s);
  return URL_FILE;
}


